package com.yanqu.road.dao.impl.guess;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.guess.data.CommonGuessItem;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonGuessItemDaoImpl extends TempDataObjectDao<CommonGuessItem> {

    public Map<Integer, Map<Integer, Map<Integer, Map<Integer, Map<String, CommonGuessItem>>>>> getData(String activityIds) {
        String selectSql = "select * from t_c_activity_common_guess_item where activityId in (" + activityIds + ")";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Integer, Map<Integer, Map<Integer, Map<Integer, Map<String, CommonGuessItem>>>>> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonGuessItem data = parse(rs);
                Map<Integer, Map<Integer, Map<Integer, Map<String, CommonGuessItem>>>> groupMap = map.computeIfAbsent(data.getActivityId(), k -> new ConcurrentHashMap<>());
                Map<Integer, Map<Integer, Map<String, CommonGuessItem>>> roundMap = groupMap.computeIfAbsent(data.getGroupId(), k -> new ConcurrentHashMap<>());
                Map<Integer, Map<String, CommonGuessItem>> siteMap = roundMap.computeIfAbsent(data.getRound(), k -> new ConcurrentHashMap<>());
                Map<String, CommonGuessItem> itemMap = siteMap.computeIfAbsent(data.getSiteId(), k -> new ConcurrentHashMap<>());
                itemMap.put(data.getObjId(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String insertSql = "INSERT INTO `t_c_activity_common_guess_item`(`activityId`, `groupId`, `round`, `siteId`, `objId`, `guessUserNum`, `createTime`) VALUES (?, ?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(CommonGuessItem data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSiteId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getObjId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuessUserNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_c_activity_common_guess_item` SET `guessUserNum` = ? WHERE `activityId` = ? and `groupId` = ? AND `round` = ? AND `siteId` = ? AND `objId` = ?";
    @Override
    protected boolean updateData(CommonGuessItem data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuessUserNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSiteId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getObjId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CommonGuessItem parseData(ResultSet rs) throws SQLException {
        CommonGuessItem data = new CommonGuessItem();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setRound(rs.getInt("round"));
        data.setSiteId(rs.getInt("siteId"));
        data.setObjId(rs.getString("objId"));
        data.setGuessUserNum(rs.getInt("guessUserNum"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }
}
